package com.example.speeddating.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.entity.Like;
import com.example.speeddating.entity.Topic;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.bouncycastle.LICENSE;

import java.util.List;

/**
 * @author tina
 * @since 2023-06-28 11:14:34
 */
@Mapper
public interface TopicMapper extends BaseMapper<Topic> {

    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "id",property = "joinNum",one = @One(select = "com.example.speeddating.mapper.ArticleMapper.selectTopicCount"))
    })
    @Select(value = "(SELECT * FROM `topic` ORDER BY hot DESC LIMIT 6) UNION ALL (SELECT * FROM topic WHERE type = 1)")
    public List<Topic> getHotTopics();


    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "id",property = "joinNum",one = @One(select = "com.example.speeddating.mapper.ArticleMapper.selectTopicCount"))
    })
    @Select("SELECT * FROM `topic` WHERE type = 0 AND (name LIKE CONCAT('%', #{searchData}, '%') OR breif LIKE CONCAT('%', #{searchData}, '%')) ORDER BY create_time DESC")
    public Page<Topic> getAllTopics(Page<Topic> topicPage,@Param("searchData") String searchData);


    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "id",property = "joinNum",one = @One(select = "com.example.speeddating.mapper.ArticleMapper.selectTopicCount"))
    })
    @Select(value = "SELECT t.id,t.`name`,t.breif,t.cover,t.hot FROM topic t,follow_topic f WHERE t.id = f.topic_id AND f.user_id = #{userId} ORDER BY f.create_time DESC")
    public Page<Topic> getUserJoinedTopic(Page<Topic> topicPage, Long userId);

    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "id",property = "joinNum",one = @One(select = "com.example.speeddating.mapper.ArticleMapper.selectTopicCount"))
    })
    @Select(value = "SELECT * FROM `topic` where id = #{topicId} and deleted = 0")
    public Topic selectTopicById(Long topicId);


    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "id",property = "joinNum",one = @One(select = "com.example.speeddating.mapper.ArticleMapper.selectTopicCount"))
    })
    @Select(value = "SELECT * FROM `topic` WHERE type = 0")
    Page<Topic> selectAllTopic(Page<Topic> pageInfo);
}
